import { getToken, setToken, removeToken } from '@/utils/auth'
import { loginAPI, getUserroleNameAPI } from '@/api'

export default {
  state: {
    token: getToken(),
    roleName1: {}
  },
  mutations: {
    setToken(state, token) {
      state.token = token
      setToken(token)
    },
    removeToken(state) {
      state.token = null
      removeToken()
    },
    setroleName(state, preload) {
      // debugger
      state.roleName1 = preload
    }
  },
  actions: {
    async login(store, data) {
      // setTimeout(() => {
      //   store.commit('setToken', '123456')
      //   console.log(data)
      // }, 500)
      const res = await loginAPI(data)
      store.commit('setToken', res.token)
    },
    async getroleName(store) {
      // debugger
      store.commit('setroleName', await getUserroleNameAPI())
    },
    // 登出
    logout(store) {
      store.commit('removeToken')
      store.commit('setroleName', {})
    }
  },
  namespaced: true
}
